package com.vcg.filters;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory;
import org.springframework.core.Ordered;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

public class ExampleGatewayFilterFactory extends AbstractGatewayFilterFactory  {

    private Integer order;

    public ExampleGatewayFilterFactory(Integer order) {
        this.order = order;
    }

    @Override
    public GatewayFilter apply(Object config) {
        return new InnerFilter() ;
    }

    private class InnerFilter implements GatewayFilter , Ordered {
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            System.out.println("  pre 自定义过滤器工厂  " + this.getClass().getSimpleName());
            // 在then方法里的，相当于aop中的后置通知
//            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
//                System.out.println("  post 自定义过滤器工厂 " + this.getClass().getSimpleName());
//            }));
            System.out.println(chain.filter(exchange));
            return chain.filter(exchange);
        }

        @Override
        public int getOrder() {
            return order;
        }
    }
}
